package org.reliablesource.prism.ui.viewers;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.reliablesource.prism.core.IPrismScreen;
import org.reliablesource.prism.core.IPrismTutorial;
import org.reliablesource.prism.core.plugin.PrismCorePlugin;
import org.reliablesource.prism.ui.composite.ImageComposite;
import org.reliablesource.prism.ui.composite.ViewMediator;
import org.reliablesource.prism.ui.plugin.PrismPlugin;

/**
 *
 *  ImageViewer.java
 *  Copyright (c) 2006, Reliable Source, Inc. All Rights Reserved<br><br>
 *
 *	Created on: Sept 19, 2006<br>
 *  @author Dennis Park 
 *  <a href="mailto:dennis.park@gmail.com">dennis.park@gmail.com</a>
 *
 *	@since 1.0
 */

public class ImageViewer extends Viewer implements ISelectionListener{

	private ImageComposite imageComposite;
	private IPrismScreen currentScreen;
	
	public ImageViewer(Composite _parent){
		imageComposite = new ImageComposite(_parent, SWT.DOUBLE_BUFFERED);
		ViewMediator.getInstance().registerPart(this);
	}
	
	@Override
	public Control getControl() {
		return imageComposite;
	}

	@Override
	public ISelection getSelection() {
		return new StructuredSelection(currentScreen);
	}

	@Override
	public void refresh() {
		if(imageComposite.isDisposed()) return;
		imageComposite.update();
	}

	@Override
	public void setSelection(ISelection _selection, boolean _reveal) {
		if(!_selection.isEmpty())			
			currentScreen = (IPrismScreen) ((StructuredSelection)(_selection)).getFirstElement();
		
		imageComposite.setImage(PrismPlugin.getDefault().readScreen(currentScreen), true);
	}
	
	
	public void forward() {
		if (currentScreen == null || imageComposite.isDisposed())
			return;

		IPrismTutorial tutorial = ((IPrismTutorial) PrismCorePlugin.getDefault().getModel()
				.getChild(0));
		
		int nImages = ((IPrismTutorial) PrismCorePlugin.getDefault().getModel()
				.getChild(0)).getChildListSize();

		int screenIndex = tutorial.getChildSequence(currentScreen);
		
//		imageCount.setText(screenIndex + "/" + nImages);

		if (screenIndex >= 0 && (screenIndex + 1) < nImages) {
			currentScreen = (IPrismScreen) tutorial.getChild(screenIndex + 1);
			imageComposite.setImage(PrismPlugin.getDefault().readScreen(currentScreen), true);
		}
	}

	public void backward() {
		if (currentScreen == null || imageComposite.isDisposed())
			return;

		int nImages = ((IPrismTutorial) PrismCorePlugin.getDefault().getModel().getChild(0)).getChildListSize();
		
		IPrismTutorial tutorial = ((IPrismTutorial) PrismCorePlugin.getDefault().getModel().getChild(0));
		
		int screenIndex = tutorial.getChildSequence(currentScreen);
//		imageCount.setText(screenIndex + "/" + nImages);
		if (screenIndex > 0) {
			currentScreen = (IPrismScreen) tutorial.getChild(screenIndex - 1);
			imageComposite.setImage(PrismPlugin.getDefault().readScreen(currentScreen), true);
		}

	}

	@Override
	public Object getInput() {
		return null;
	}

	@Override
	public void setInput(Object input) {
	}

	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		
		StructuredSelection changedSelectionTo = (StructuredSelection) selection;
		int nItems = changedSelectionTo.size();
		
		if (changedSelectionTo.getFirstElement().equals(currentScreen) && 
				nItems != 1 )
			return;

		if (changedSelectionTo.getFirstElement() instanceof IPrismScreen) {
			currentScreen = (IPrismScreen) changedSelectionTo.getFirstElement();
			setSelection(changedSelectionTo, true);
		}		
	}
	
}
